package com.orhon.smartcampus.modules.schoolaffairs.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolChartSayingDao;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolOpinionDao;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolChartSaying;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolOpinion;
import com.orhon.smartcampus.modules.schoolaffairs.gqlModel.GQLSchoolChartSaying;
import com.orhon.smartcampus.modules.schoolaffairs.gqlModel.GQLSchoolOpinion;
import com.orhon.smartcampus.modules.schoolaffairs.nm.SchoolChartSayingMapper;
import com.orhon.smartcampus.modules.schoolaffairs.nm.SchoolOpinionMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class SchoolChartSayingsFetcher implements DataFetcher {
    @Autowired
    TokenService tokenService;

    @Autowired
    SchoolChartSayingDao schoolChartSayingDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
        String hql = "from SchoolChartSaying where deleted_at = null "+" and school_id = " + schoolId;
        List<GQLSchoolChartSaying> GQLSchoolOpinion = new ArrayList<>();
        if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
            if(map.get("mkState")!=null) {
                hql += " and mkState" + Util.decide(map.get("mkState"));
            }
//            if(map.get("eclass_id")!=null) {
//                hql += " and eclass" + Util.decide(map.get("eclass_id"));
//            }
//            if(map.get("departments_id")!=null) {
//                hql += " and departments" + Util.decide(map.get("departments_id"));
//            }
//            if(map.get("title")!=null){
//                hql += " and title like '%"+map.get("title")+"%'";
//            }
        }
        List<SchoolChartSaying> list = schoolChartSayingDao.find(hql, null, page.getPage(), page.getLimit());
        for(SchoolChartSaying chartSaying:list){
//            GQLSchool gqlSchool = null;
//            if (env.getSelectionSet().contains("data/school")){
//                opinion.getSchoolsBySchool();
//                Schools schools = opinion.getSchoolsBySchool();
//                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
//            }
//            GQLEclass gqlEclass=null;
//            if(env.getSelectionSet().contains("data/eclass")){
//               // opinion.getEclass();
//                Eclass eclass = opinion.getEclass();
//                gqlEclass = EclassMapper.MAPPER.toEclass(eclass);
//            }
            GQLUser gqlUser = null;
            if(env.getSelectionSet().contains("data/publisher")){
                Users publisher = chartSaying.getPublisher();
                gqlUser= UserMapper.MAPPER.mapUser(publisher);

            }
            GQLSchoolChartSaying gqlSchoolOpinion = SchoolChartSayingMapper.MAPPER.toSchoolChartSaying(chartSaying);
            //gqlSchoolNotice.setEclass(gqlEclass);
            gqlSchoolOpinion.setPublisher(gqlUser);
            GQLSchoolOpinion.add(gqlSchoolOpinion);
        }
        int total = 0;
        if (env.getSelectionSet().contains("total")){
            total = schoolChartSayingDao.total(hql,null);
        }
        return Result.ok().put("data", GQLSchoolOpinion).put("total", total);
    }
}
